package fmtlog

import "bytes"

// buffer holds a byte Buffer for reuse. The zero value is ready for use.
type buffer struct {
    bytes.Buffer

    tmp    [64]byte // temporary byte array for creating headers.
    next   *buffer
}

// Some custom tiny helper functions to print the log header efficiently.
const DIGITS = "0123456789"

// twoDigits formats a zero-prefixed two-digit integer at buf.tmp[i].
func (buf *buffer) twoDigits(i, d int) {
	buf.tmp[i+1] = DIGITS[d%10]
	d /= 10
	buf.tmp[i] = DIGITS[d%10]
}

// nDigits formats an n-digit integer at buf.tmp[i], padding with pad on the left. It assumes d >= 0.
func (buf *buffer) nDigits(n, i, d int, pad byte) {
	j := n - 1
	for ; j >= 0 && d > 0; j-- {
		buf.tmp[i+j] = DIGITS[d%10]
		d /= 10
	}
	for ; j >= 0; j-- {
		buf.tmp[i+j] = pad
	}
}

// someDigits formats a zero-prefixed variable-width integer at buf.tmp[i].
func (buf *buffer) someDigits(i, d int) int {
	j := len(buf.tmp)
	for d > 0 {
		j--
		buf.tmp[j] = DIGITS[d%10]
		d /= 10
	}
	return copy(buf.tmp[i:], buf.tmp[j:])
}

